﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRM
{
    public partial class AddUserMes : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录..."); 
            
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("1_2") < 0)
            {
                Common.MessageBox.Show(Page, "你没有编辑的权限...", "UserService.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSaveUserInfo_Click(object sender, EventArgs e)
        {
            //创建客户信息实体对象
            Model.DataUserInfo UserModel = new CRM.Model.DataUserInfo();
            //根据用户名是否为空判断客户的状态，即客户是否被分配了跟踪用户
            UserModel.T_UserState = (string.IsNullOrEmpty
                (Common.EncryptString.DeMyEnncrypt
                (Common.GetMes.GetCookies("User_Name"), 6))) ? 0 : 1;
            //客户类型
            UserModel.T_UserType = Convert.ToInt32(DDlClass.SelectedValue);
            //客户来源
            UserModel.T_UserFrom = Convert.ToInt32(DDLFrom.SelectedValue);
            //客户等级
            UserModel.T_UserLeave = Convert.ToInt32(DDLLeave.SelectedValue);
            //描述
            UserModel.T_Remark = Server.HtmlEncode(txtRemark.Value.Trim());
            //省份
            UserModel.T_Province = Server.HtmlEncode(Province.Value);
            UserModel.T_City = Server.HtmlEncode(City.Value);
            UserModel.T_Tel = Server.HtmlEncode(txtTel.Value.Trim());
            UserModel.T_QQ = Server.HtmlEncode(txtQQ.Value);
            UserModel.T_Sex = (RBMan.Checked) ? "男" : "女";
            UserModel.T_PostCode = Server.HtmlEncode(txtPost.Value.Trim());
            UserModel.T_WebUrl = Server.HtmlEncode(txtWebUrl.Value.Trim());
            UserModel.T_CompanyName = Server.HtmlEncode(txtCompanyName.Value.Trim());
            UserModel.T_Email = Server.HtmlEncode(txtMail.Value.Trim());
            UserModel.T_Fax = Server.HtmlEncode(txtFax.Value.Trim());
            UserModel.T_Mobile = Server.HtmlEncode(txtMoblie.Value.Trim());
            UserModel.T_Address = Server.HtmlEncode(txtAdress.Value.Trim());
            UserModel.T_Person = Server.HtmlEncode(txtPerson.Value.Trim());
            //设置当前跟踪的用户
            UserModel.T_NowService = (string.IsNullOrEmpty
                (Common.EncryptString.DeMyEnncrypt
                (Common.GetMes.GetCookies("User_Name"), 6))) ? "" : 
                Common.EncryptString.DeMyEnncrypt
                (Common.GetMes.GetCookies("User_Name"), 6);
            UserModel.T_UserID = 0;
            string NewUserID = string.Empty;
            //将客户信息保存到数据库中
            if (BLL.SysBase.AddNewsData(UserModel, out NewUserID))
            {  //向跟踪日志表中插入工作记录信息
                BLL.ServiceUser.InsertWorkLog
                    (NewUserID, Common.GetMes.GetSession("User_ID"));
                //提示信息保存成功，并要求用户是否继续添加
                Common.MessageBox.myScriptMes(Page,
                    "if(confirm(\"信息保存成功，是否继续添加？否则返回信息列表。\")) "+
                    "{location.href=\"AddUserMes.aspx\"} else  "+
                    "{location.href=\"UserService.aspx\"} ", "Suess");
            }
            else
            {   //保存失败，显示提示信息
                Common.MessageBox.Show(Page, 
                    "信息保存失败，请重试或联系技术人员......",
                    "UserService.aspx","Error");
            }
        }
    }
}
